package com.mlj.controller;

import cn.dsna.util.images.ValidateCode;
import com.mlj.pojo.Temp;
import com.mlj.pojo.User2;
import com.mlj.service.UserService;
import com.mlj.util.Md5Util;
import com.mlj.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;

@Controller
@RequestMapping("/home")
public class HomeController {
    @Autowired
    UserService userService;

    @RequestMapping("/changePassword_page")
    public String changePassword_page(){
        return "updatePassword";
    }

    /**
     * 生成验证码
     */
    @RequestMapping("/getCode")
    public String getCode(HttpServletResponse response, HttpServletRequest request){
        //生成验证码 ValidateCode.jar
        //四个参数：宽、高、字符数量、干扰线数量
        ValidateCode validateCode = new ValidateCode(120, 40, 5, 10);
        String code = validateCode.getCode();
        //将系统生成的验证码存放到session中
        HttpSession session = request.getSession();
        session.setAttribute("code",code);
        //将生成的验证码输出到前端页面
        try {
            validateCode.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return code;
    }

    @RequestMapping("/")
    public String login(){
        return "login";
    }

    @RequestMapping("/user_login")
    public String main(HttpServletRequest request, Model model, HttpSession session) {
        String userName = request.getParameter("login_name");
        String password = request.getParameter("password");
        System.out.println(password);
        String code = request.getParameter("code");
        String message="";
        //从session中获取验证码
        String codeValue = (String)session.getAttribute("code");
        //先比较验证码(忽略大小写比较)
        if(!code.equalsIgnoreCase(codeValue)){
            //给出验证码输入错误的提示
            message="验证码错误，请重新输入";
            model.addAttribute("loginName", userName);
            model.addAttribute("code", code);
            model.addAttribute("password", password);
            model.addAttribute("message",message);
            return "login";
        }else{
            //比较账号和密码
            //根据账号查询用户
            User2 user2 = userService.selectUser(userName);
            //System.out.println(user2.getUser_password());
            //System.out.println(Md5Util.secretPass(userName,password));
            if(user2==null || !user2.getUser_password().equals(Md5Util.secretPass(userName,password))){
                message="账号或者密码输入错误";
                model.addAttribute("loginName", userName);
                model.addAttribute("code", code);
                model.addAttribute("password", password);
                model.addAttribute("message",message);
                return "login";
            }else{
                //将用户信息存放到session中
                session.setAttribute("User",user2);
                // session.setMaxInactiveInterval(1000);
                return "main";
            }
        }
    }
    //注销
    @RequestMapping("/login_out")
    public String adminLogout(HttpServletRequest request) {
        // 删除所有session
        Enumeration em = request.getSession().getAttributeNames();
        while (em.hasMoreElements()) {
            request.getSession().removeAttribute(em.nextElement().toString());
        }
        return "redirect:/home/";
    }
    //修改密码
    @RequestMapping("/changePassword")
    @ResponseBody
    public Result changePassword(Temp temp, HttpSession session){
        Result result = userService.changePassword(temp,session);
        return result;
    }
}
